package com.uchengwang.ui.cm_ui.cycle.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.uchengwang.R;
import com.uchengwang.functions.db.manager.LiteOrmDBUtil;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.listener.OnResultListener;
import com.uchengwang.functions.image.ImageManager;
import com.uchengwang.ui.cm_ui.cycle.bean.CycleBean;
import com.uchengwang.ui.cm_ui.cycle.http.CycleHttpUtils;
import com.uchengwang.ui.cm_ui.cycle.ui.LoadCycleWebHtmlActivity;
import com.uchengwang.ui.cm_ui.uu.bean.XiangliaoBean;
import com.uchengwang.ui.cm_ui.uu.ui.XiangliaoActivity;
import com.uchengwang.ui.cm_ui.uu.ui.XiangliaoChooseMoodActivity;
import com.uchengwang.ui.utils.LoadingUtils;
import com.uchengwang.ui.utils.UCommonUtil;
import com.umeng.analytics.MobclickAgent;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by U城网 on 2018/4/25.
 * 社圈新主页侧滑 显示的界面
 */

public class CycleMainAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private Context context;
    private List<CycleBean> data = new ArrayList<>();


    public void setData(List<CycleBean> data){
        this.data = data;
        notifyDataSetChanged();
    }

    public List<CycleBean> getData(){
        return data;
    }

    public CycleMainAdapter(Context context){
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_cycle_main, parent, false);
        return new MainVH(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder h, int position) {
        MainVH holder = (MainVH) h;

        final CycleBean bean = data.get(position);
        if (TextUtils.isEmpty(bean.getCycleCover())){
            ImageManager.getInstance().loadImage(context, holder.main_head, bean.getResId(), ImageManager.ImageType.CIRCLE);
        }else {
            ImageManager.getInstance().loadImage(context, holder.main_head, bean.getCycleCover(), ImageManager.ImageType.CIRCLE);
        }

        holder.main_name.setText(bean.getCycleName());

        holder.main_desc.setText(bean.getCycleDesc());

        if (bean.getUid() ==UCommonUtil.getUtil().getId()){
            holder.main_quanzhu.setVisibility(View.VISIBLE);
        }else {
            holder.main_quanzhu.setVisibility(View.GONE);
        }


        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = null;
                switch (bean.getCycleName()){
                    case "享聊":
                        List<XiangliaoBean> beanList = LiteOrmDBUtil.getLiteOrm(context).getQueryByWhere(XiangliaoBean.class, "uid", String.valueOf(UCommonUtil.getUtil().getId()));
                        XiangliaoBean xiangliaoBean = null;
                        if (beanList != null && beanList.size() >0){
                            xiangliaoBean = beanList.get(0);
                        }

                        if (xiangliaoBean == null || ((System.currentTimeMillis()-xiangliaoBean.getCreateTime()) >24*60*60*1000)){
                            intent = new Intent(context, XiangliaoChooseMoodActivity.class);
                        }else {
                            intent = new Intent(context, XiangliaoActivity.class);
                        }

//                        long enterTime = SPUtils.getPreferences(context).getEnterEnjoyChatTime();
//                        if ((System.currentTimeMillis()-enterTime) >24*60*60*1000){
//                            //距离上次进入享聊  超过一个小时  重新进入
////                            intent = new Intent(context, EnjoyChatEnterActivity.class);
//                            intent = new Intent(context, XiangliaoChooseMoodActivity.class);
//                        }else {
////                            intent = new Intent(context, EnjoyChatListActivity.class);
//                            intent = new Intent(context, XiangliaoActivity.class);
//                        }
                        MobclickAgent.onEvent(context,"xiangLiao");
                        break;
                    case "享玩":
                        intent = new Intent(context, LoadCycleWebHtmlActivity.class);
                        intent.putExtra(BaseConfig.URL_TITLE, "享玩");
                        intent.putExtra(BaseConfig.URL, BaseConfig.BaseWebURL + "/plays");
                        intent.putExtra(BaseConfig.IS_HIDE_RIGHT, true);
                        MobclickAgent.onEvent(context,"xiangWan");
                        break;
                    case "伴旅":

                        break;
                    case "会场":
                        intent = new Intent(context, LoadCycleWebHtmlActivity.class);
                        intent.putExtra(BaseConfig.PUB_TYPE, 2);
                        intent.putExtra(BaseConfig.URL_TITLE, "会场");
                        intent.putExtra(BaseConfig.URL, BaseConfig.BaseWebURL+"/events");
                        MobclickAgent.onEvent(context,"huiChang");
                        break;
                    default://打开h5的社圈主页
                        if (bean != null){
                            intent = new Intent(context, LoadCycleWebHtmlActivity.class);
                            intent.putExtra(BaseConfig.URL, BaseConfig.BaseWebURL+"/cycle/"+bean.getId());
//                            intent.putExtra(BaseConfig.URL_TITLE, bean.getCycleName());
//                            intent.putExtra(BaseConfig.IS_HIDE_RIGHT, true);
                        }

                        break;
                }

                if (intent != null){
                    context.startActivity(intent);
                }

            }
        });

        if (bean.isNewJoin()){
            holder.main_new_cycle_apply.setVisibility(View.VISIBLE);
        }else {
            holder.main_new_cycle_apply.setVisibility(View.GONE);
        }

        holder.main_new_cycle_apply.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (bean != null){
                    LoadingUtils.getUtils().loading(context);
                    CycleHttpUtils.getUtils().getCycleNewApply((Activity) context, bean.getId(), true, new OnResultListener() {
                        @Override
                        public void onResult(int stutas, String msg, int total, String data) {
                            LoadingUtils.getUtils().hide();

                            Intent intent = new Intent(context, LoadCycleWebHtmlActivity.class);
                            intent.putExtra(BaseConfig.URL, BaseConfig.BaseWebURL+"/cycle/" + bean.getId()+ "/join");
                            intent.putExtra(BaseConfig.IS_FULL, true);
                            intent.putExtra(BaseConfig.IS_HIDE_RIGHT, true);
                            context.startActivity(intent);

                            bean.setNewJoin(false);
                            notifyDataSetChanged();
                        }
                    });

                }
            }
        });


//        holder.main_release.setOnClickListener(new View.OnClickListener(){
//            @Override
//            public void onClick(View view) {
//                //弹出popupwindow
//                List<SnsBean> list = new ArrayList<>();
//                list.add(new SnsBean(R.mipmap.uchenglogo, "享说"));
//                list.add(new SnsBean(R.mipmap.uchenglogo, "视频"));
//                list.add(new SnsBean(R.mipmap.uchenglogo, "投票"));
//
//                SnsPopupWindow window = new SnsPopupWindow(context, list);
//                window.showPopupWindow(view);
//            }
//        });


    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    class MainVH extends RecyclerView.ViewHolder {
        private View itemView;
        private ImageView main_head, main_release, main_quanzhu;
        private TextView main_name, main_desc, main_new_cycle_apply;
//        private SnsPopupWindow window;

        public MainVH(View view) {
            super(view);
            itemView = view;
            AutoUtils.auto(view);

            main_head = view.findViewById(R.id.main_head);
            main_release = view.findViewById(R.id.main_release);
            main_name = view.findViewById(R.id.main_name);
            main_desc = view.findViewById(R.id.main_desc);
            main_new_cycle_apply = view.findViewById(R.id.main_new_cycle_apply);
            main_quanzhu = view.findViewById(R.id.main_quanzhu);



        }
    }
}
